/**
 * Created by buzhizhe on 2019/1/19.
 */
var controllerName="/[tblName]";
var VM=new Object();
var loading = false;
var loadall=false;


$(document).ready(function() {
    initDataTable();
    addEvents();
});

//加载事件
function addEvents() {
    //滚动加载事件
    $(document.body).infinite().on("infinite", function() {
        if(loading) return;
        loading = true;
        setTimeout(function() {
            getMoreData();
            loading = false;
        }, 1000);
    });

    //行点击事件
    $(document).on("click", ".tbl-row", function() {
        var id=$(this)[0].attributes["data-id"].value;
        $.actions({
            title: "选择操作",
            onClose: function() {
                console.log("close");
            },
            actions: [
                {
                    text: "查看",
                    className: "color-warm",
                    onClick: function() {
                        popupView(id);
                    }
                },
                {
                    text: "编辑",
                    className: "color-primary",
                    onClick: function() {
                        popupAdd(id);
                        //$(location).attr('href', '/[tblName]/add/'+id);
                    }
                },
                {
                    text: "删除",
                    className: 'color-danger',
                    onClick: function() {
                        doDelete(id);
                    }
                }
            ]
        });

    });

	/*
    //状态处理
    $("#txtshowstatus").select({
        title: "选择状态",
        items: arr_items["status"],
        onClose: function(d) {
            $("#txt-status").val(d.data.values);
            console.log(d.data.values);
        }
    });
	*/

}
//初始化VM
function initDataTable() {
    VM = new Vue({
        delimiters: ["[[", "]]"],
        el: '#app',
        data: {listdata: []}
    });
    $("#pageindex").val(0);
    getMoreData();
}
//加载分页数据
function getMoreData() {
    if(loadall){
        $("#loadingok").show();
        //$.toast("没有更多记录了", "text");
        return;
    }else {
        //$.showLoading();
        $("#loading").show();
    }
    var pageindex=parseInt($("#pageindex").val())+1;
    var pagesize=parseInt($("#pagesize").val());
    $("#pageindex").val(pageindex);
    var result=getListData();
    var code=result.statecode;
    if(code==0){
        $("#loading").hide();
    	if(result.data==""){loadall=true;getMoreData();return;}
        result.data.forEach(function(v){
            //状态赋值
            //v.statusname=getTitleByValue(arr_items["status"],v.status);
            VM.listdata.push(v);
        });
        //$.hideLoading();
        if(result.data.length<pagesize){loadall=true;}
    } else{
        $("#loading").hide();
        //$.hideLoading();
        $.alert("接口返回错误：" + result.message);
    }

}
//ajxa获取列表数据
function getListData() {
    var url = domain + controllerName + "/index/1";
    var ret = [];
    $.ajax({
        type: "POST",
        url: url,
        async: false,
        data:$('#formQuery').serialize(),// 你的formid
        error: function (request) {
            alert("网络连接错误");
        },
        success: function (result) {
            ret=result;
            //alert(typeof _self.ret.data);
        }
    });
    return ret;
}

//打开表单
function popupAdd(id) {
    if(id==undefined)id=0;
    var url= domain + controllerName + "/get/"+id
    $.ajax({
            type: "POST",
            url: url,
            error: function (request) {
                $.alert("网络连接错误");
            },
            success: function (result) {
                //console.log(result);
                var code=result.statecode;
                if(code==0){
                    var data=result.data;
                    //data["showstatus"]=getTitleByValue(arr_items["status"],data["status"]);
                    for (var k in data) {
                        //console.log(data[k]);
                        $("input[name='"+k+"']").val(data[k]);
                    }
                    $("#layer-add").popup();
                }else{
                    $.alert("获取失败："+result.message);
                }
            }
        }
    );
}
//打开表单
function popupView(id) {
    if(id==undefined)id=0;
    var url= domain + controllerName + "/get/"+id
    $.ajax({
            type: "POST",
            url: url,
            error: function (request) {
                $.alert("网络连接错误");
            },
            success: function (result) {
                //console.log(result);
                var code=result.statecode;
                if(code==0){
                    //data["statusname"]=getTitleByValue(arr_items["status"],data["status"]);
                    var data=result.data;
                    for (var k in data) {
                        //console.log(data[k]);
                        $("span[name='"+k+"']").html(data[k]);
                    }
                    $("#layer-view").popup();
                }else{
                    $.alert("获取失败："+result.message);
                }
            }
        }
    );
}

//查询
function formQuerySubmit() {
    $("#pageindex").val(0);
    VM.listdata.length=0;
    loadall=false;
    getMoreData();
    //$("#formQuery").submit();
}
//保存
function formAddSubmit() {
    var url= domain + controllerName + "/add"
    $.ajax({
            type: "POST",
            url: url,
            data: $('#formAdd').serialize(),// 你的formid
            error: function (request) {
                $.alert("网络连接错误");
            },
            success: function (result) {
                //console.log(data);
                var code=result.statecode;
                if(code==0){
                    $.closePopup("#layer-add");
                    $.alert("提交成功", "提示", function() {
                        $("#formQuery")[0].reset();
                        formQuerySubmit();
                    });
                }else{
                    $.alert("提交失败："+result.message);
                }
            }
        }
    );

}
//删除
function doDelete(id) {
    var url= domain + controllerName + "/delete/"+id
    $.ajax({
            type: "POST",
            url: url,
            error: function (request) {
                $.alert("网络连接错误");
            },
            success: function (result) {
                //console.log(data);
                var code=result.statecode;
                if(code==0){
                    $.alert("提交成功", "提示", function() {
                        formQuerySubmit();
                        //location.href=controllerName + "/list";
                        //$(location).attr('href', '/[tblName]/list/');
                    });
                }else{
                    $.alert("提交失败："+result.message);
                }
            }
        }
    );

}
